import ajax from '@/utils/ajax'
import dialog from '@/utils/dialog'
import cookie from '@/utils/cookie'

export default function (data) {
  const json = {
    iphone: data.tel,
    code: data.numcode,
    type: 2,
  }
  return ajax({
    url: `/login`,
    loading: '登录中...',
    header:{
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: {
      json: JSON.stringify(json)
    }
  }).then(resdata => {
    if (resdata.token) {
      cookie.setCookie('name', resdata.name)
      cookie.setCookie('token', resdata.token)
      return resdata
    }else{
      throw resdata.msg || '登录失败'
    }
  }).catch((msg) => dialog.showToast(msg || '请求失败'))
}
